package com.dek.seckill.controller;


import com.dek.seckill.common.redis.GoodsKey;
import com.dek.seckill.common.redis.RedisService;
import com.dek.seckill.service.GoodsService;
import com.dek.seckill.vo.GoodsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring5.context.webflux.SpringWebFluxContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class GoodsController {


    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisService redisService;
    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;



    @RequestMapping(value = "goods_list", produces = "text/html")
    @ResponseBody
    public String goodsList(Model model, HttpServletRequest request, HttpServletResponse response) {
        List<GoodsVO> list = goodsService.goodsVOList();
        model.addAttribute("goodsList", list);

        /**
        String html = redisService.get(GoodsKey.getGoodsList,"", String.class);
        if (StringUtils.isBlank(html)) {
            return html;
        }
        //手动渲染
        SpringWebFluxContext

        html = thymeleafViewResolver.getTemplateEngine().process("goods_list", context);
        if(StringUtils.isNotBlank(html)) {
            redis.set
         }
        **/


        return "goods_list";
    }

    @RequestMapping("detail/{goodsId}")
    public String goodsList(Model model, @PathVariable("goodsId") Long goodsId) {
        GoodsVO goodsVO = goodsService.getDetail(goodsId);
        model.addAttribute("goods", goodsVO);


        Long startTime = goodsVO.getStartDate().getTime();
        Long endTime = goodsVO.getEndDate().getTime();
        Long now = System.currentTimeMillis();


        model.addAttribute("seckillStatus", 1);
        model.addAttribute("remainSeconds", 0);
        return "goods_detail";
    }

}
